package com.bizmerce.rnd.oms.codemng.web;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bizmerce.rnd.oms.codemng.service.CodeMngService;
import com.bizmerce.rnd.oms.codemng.service.DeMinimisParamVO;
import com.bizmerce.rnd.oms.codemng.service.DeMinimisVO;
import com.bizmerce.rnd.oms.codemng.service.FtaSearchVO;
import com.bizmerce.rnd.oms.codemng.service.FtaVO;
import com.bizmerce.rnd.oms.codemng.service.HsCodeSearchVo;
import com.bizmerce.rnd.oms.codemng.service.HsCodeVO;
import com.bizmerce.rnd.oms.codemng.service.OffshoreVO;
import com.bizmerce.rnd.oms.codemng.service.ViewVO;

import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;

@Controller
public class CodeMngController {

	Log log = LogFactory.getLog(CodeMngController.class);

	@Resource(name = "codeMngService")
	CodeMngService codeMngService;

	/**
	 * FTA협정관리 - 정보 가져오기
	 */
	@RequestMapping("/codeMng/ftamng_select.do")
	public String select(@ModelAttribute FtaSearchVO searchVO, Model model){
		List<FtaVO> result = codeMngService.selectFtas(searchVO);
		model.addAttribute("ftas", result);
		
		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
		paginationInfo.setPageSize(searchVO.getPageSize());
		paginationInfo.setTotalRecordCount(codeMngService.selectFtasCount());
		
		model.addAttribute("paginationInfo", paginationInfo);
		
		return "codemng/oms01";
	}
	
	/**
	 * FTA협정관리 - 적용
	 */
	@RequestMapping("/codeMng/ftamng_apply.do")
	public String apply(@ModelAttribute FtaSearchVO searchVO, Model model) {
		if(searchVO.getIds() != null) {
			codeMngService.apply(searchVO);	
		}
		return "forward:/codeMng/ftamng_select.do";
	}

	/**
	 * FTA협정관리 - 미적용
	 */
	@RequestMapping("/codeMng/ftamng_noapply.do")
	public String noApply(@ModelAttribute FtaSearchVO searchVO, Model model) {
		if(searchVO.getIds() != null) {
			codeMngService.noApply(searchVO);
		}
		return "forward:/codeMng/ftamng_select.do";
	}

	/**
	 * FTA협정관리 - 협정내역 정보 가져오기
	 */
	@RequestMapping(value = "/codeMng/ftamng_selectfta.do", params = { "id" })
	public String selectFta(@ModelAttribute FtaSearchVO searchVO, Model model) {
		FtaVO result = codeMngService.selectFta(searchVO.getId());	
		model.addAttribute("fta", result);
		return "codemng/oms02";
	}

	/**
	 * FTA협정관리 - 협정내역 저장
	 */
	@RequestMapping("/codeMng/ftamng_updatefta.do")
	public String updateFta(@ModelAttribute FtaVO fta, Model model) {

		if (log.isDebugEnabled()) {
			log.debug(ToStringBuilder.reflectionToString(fta));
		}

		codeMngService.updateFta(fta);
		return "forward:/codeMng/ftamng_selectfta.do";
	}

	/**
	 * 역외가공기준 - 세번부호 정보가져오기
	 */
	@RequestMapping("/codeMng/offmng_selectoff.do")
	public String selectOff(@ModelAttribute FtaSearchVO searchVO, Model model) {
		String ftaId = searchVO.getId();
		String hsCode = searchVO.getHsCode();
		
		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
		paginationInfo.setPageSize(searchVO.getPageSize());
		paginationInfo.setTotalRecordCount(codeMngService.selectOffCount(searchVO));
		
		List<OffshoreVO> result = codeMngService.selectOffById(searchVO);
		model.addAttribute("offshore", result);
		model.addAttribute("paginationInfo", paginationInfo);
		return "codemng/oms03";
	}

	/**
	 * 누적기준 적용여부
	 */
	@RequestMapping("/codeMng/cummng_selectcum.do")
	public String selectCum(@ModelAttribute FtaSearchVO ftaVO, Model model) {
		if (log.isDebugEnabled()) {
			log.debug(ftaVO.getId());
		}
		FtaVO result = codeMngService.selectCum(ftaVO.getId());
		model.addAttribute("apply", result);
		return "codemng/oms04";
	}

	/**
	 * 누적기준 - 저장
	 */
	@RequestMapping("/codeMng/cummng_apply.do")
	public String updateCum(@ModelAttribute FtaSearchVO ftaVO, Model model) {
		if (log.isDebugEnabled()) {
			log.debug(ToStringBuilder.reflectionToString(ftaVO));
		}
		codeMngService.updateCum(ftaVO.getId(), ftaVO.isApplyAccumulation());
		return "forward:/codeMng/cummng_selectcum.do";
	}
		
	/**
	 * 미소기준 - 정보 가져오기
	 */
	@RequestMapping("/codeMng/demin_selectdemin.do")
	public String selectDeMin(@ModelAttribute FtaSearchVO ftaVO, Model model){
		if (log.isDebugEnabled()) {
			log.debug("##" + ToStringBuilder.reflectionToString(ftaVO));
		}
		List<DeMinimisVO> result = codeMngService.selectDeMin(ftaVO.getId());
		
		
		
		model.addAttribute("demin", result);
		return "codemng/oms05";
	}

	/**
	 * 미소기준 -저장
	 */
	@RequestMapping("/codeMng/demin_updatedemin.do")
	public String updateDeMin(@ModelAttribute FtaSearchVO ftaVO, DeMinimisParamVO dem, Model model) {
		codeMngService.updateDeMin(ftaVO.getId(), dem);
		return "forward:/codeMng/demin_selectdemin.do";
	}

	/**
	 * 품목별기준 - 정보 가져오기
	 */
	@RequestMapping("/codeMng/viewmng_selectview.do")
	public String selectView(@ModelAttribute FtaSearchVO searchVO, Model model){
		
		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
		paginationInfo.setPageSize(searchVO.getPageSize());
		paginationInfo.setTotalRecordCount(codeMngService.selectViewCount(searchVO));
		
		List<ViewVO> result = codeMngService.selectView(searchVO);
		model.addAttribute("view", result);
		model.addAttribute("paginationInfo", paginationInfo);
		return "codemng/oms06";
	}
	
	/**
	 * HSCode 관리 - 세번부호 정보가져오기
	 */
	@RequestMapping("/codeMng/hs_selecthscode.do")
	public String selectHsCds(@ModelAttribute FtaSearchVO searchVO, Model model){
		if (log.isDebugEnabled()) {
			log.debug("##" + ToStringBuilder.reflectionToString(searchVO));
		}
		
		List<HsCodeVO> result = codeMngService.selectHsCds(searchVO);
		model.addAttribute("hscode", result);
		
		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(searchVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(searchVO.getPageUnit());
		paginationInfo.setPageSize(searchVO.getPageSize());
		paginationInfo.setTotalRecordCount(codeMngService.selectHsCdCount(searchVO));
		
		model.addAttribute("paginationInfo", paginationInfo);
		
		return "codemng/oms07";
	}
}